<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>表格</title>
    
    <style>
        table{
            border: 1px solid;
            margin: auto;
            width: 500px;
        }

        td,th{
            text-align: center;
            border: 1px solid;
        }
        div{
            text-align: center;
            margin: 50px;
        }
    </style>
</head>
<body>
    <input type="button" value="添加" id="add" style="width: 10%; margin: auto;">

    <table>
        
        <tr style="background-color: black;">
            <td>&nbsp;</td>
        </tr>
        <tr style="background-color: wheat;">
            <td>&nbsp;</td>
        </tr>
    </table>

    <script>
        //添加到表格
        document.querySelector("input").onclick = function(){
            let Alltr = document.querySelectorAll("tr");
            let tr;
            if(Alltr.length%2==0){
                tr =`<tr style="background-color: black;">
                    <td>&nbsp;</td>
                </tr>`;
            }else{
                tr =`<tr style="background-color: wheat;">
                    <td>&nbsp;</td>
                </tr>`;
            }
            document.querySelector("tbody").innerHTML+=tr;
        }
    </script>
</body>
</html>